package com.wuji.salary.service.impl;

import com.wuji.salary.dao.AlgorithmDao;
import com.wuji.salary.entity.Algorithm;
import com.wuji.salary.entity.R;
import com.wuji.salary.service.AlgorithmService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * (Algorithm)表服务实现类
 *
 * @author makejava
 * @since 2024-07-27 10:43:11
 */
@Service("algorithmService")
public class AlgorithmServiceImpl implements AlgorithmService {
    @Autowired
    private AlgorithmDao algorithmDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Algorithm queryById(Integer id) {
        return this.algorithmDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<Algorithm> queryByPage() {
        return this.algorithmDao.queryAllByLimit();
    }

    /**
     * 新增数据
     *
     * @param algorithm 实例对象
     * @return 实例对象
     */
    @Override
    public R insert(Algorithm algorithm) {
        if (algorithmDao.getPayrollAlgorithmByName(algorithm.getAlgorithmName()) != null){
            return R.fail("算法名不能重复");
        }else {
            return R.ok(this.algorithmDao.insert(algorithm));
        }
    }

    /**
     * 修改数据
     *
     * @param algorithm 实例对象
     * @return 实例对象
     */
    @Override
    public void update(Algorithm algorithm) {
        this.algorithmDao.update(algorithm);
    }

    /**
     * 通过主键删除数据
     *
     * @param algorithmName
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String algorithmName) {
        return this.algorithmDao.deleteById(algorithmName) > 0;
    }
}
